package com.diipo.weibo;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

/**
 * 异步数据加载
 * 
 * @author starry
 * 
 */
public class AsyncDataLoader extends AsyncTask<Void, Long, Object> {
	private AsyncDataLoader.Callback mCallback;
	private String TAG = AsyncDataLoader.class.getSimpleName();
	private ProgressDialog progressDialog;
	private Context ctx;
	private String mUrl;
	private String mArg1;
	private String mArg2;
	private String mMore;

	/*
	 * arg1:   type 类型
	 * arg2:   status 值
	 * more:   加载更多是url的参数
	 * */
	public AsyncDataLoader(AsyncDataLoader.Callback callback,Context contex,String url,String arg1,String arg2,String more) {
		setCallback(callback);
		ctx = contex;
		mUrl = url;
		mArg1 = arg1;
		mArg2 = arg2;
		mMore = more;
	}

	@Override
	protected Object doInBackground(Void... voids) {
		if (mCallback != null) {
			mCallback.onStart(mUrl,mMore);
			mCallback.onStart(mUrl,mArg1,mArg2);
		}
		return null;
	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		progressDialog.dismiss();
		if (mCallback != null) {
			mCallback.onFinish();
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog = new ProgressDialog(ctx);
		progressDialog.setMessage("正在加载中...");
		progressDialog.show();
		if (mCallback != null) {
			mCallback.onPrepare();
		}
	}

	public void setCallback(AsyncDataLoader.Callback callback) {
		this.mCallback = callback;
	}

	public interface Callback {
		public void onPrepare();

		public void onStart(String address,String more);
		public void onStart(String address,String arg1,String arg2);

		public void onFinish();
	}

	void Log(String msg) {
		Log.i(TAG, "Asyc---" + msg);
	}

}
